const cityurl = 'https://geoapi.qweather.com/v2/city/lookup?key=0a81e9d657e6423a95376f18afa9f019&location='
const weatherurl = 'https://devapi.qweather.com/v7/weather/now?key=0a81e9d657e6423a95376f18afa9f019&location='
const futureurl = 'https://devapi.qweather.com/v7/weather/3d?key=0a81e9d657e6423a95376f18afa9f019&location='
Page({
  data: {
    city: '',
    temperature: '',
    cityid: '',
    wind: '',
    shidu: '',
    tqzk: '',
    fengli: '',
    qiya: '',
    daily: []
  },
  input(e) {
    console.log(e)
    let city = e.detail.value
    this.setData({
      city: city
    })
  },
  search() {
    let url = cityurl + this.data.city
    console.log('cityidurl:', url)
    let that = this
    wx.request({
      url,
      success(e) {
        console.log('cityid:', e)
        let cityid = e.data.location[0].id
        that.setData({
          cityid
        })
        let wurl = weatherurl + cityid
        console.log('weatherurl:', url)
        wx.request({
          url: wurl,
          success(e) {
            console.log('weather:', e)
            let temperature = e.data.now.temp
            let wind = e.data.now.windDir
            let tqzk = e.data.now.text
            let fengli = e.data.now.windScale
            let shidu = e.data.now.humidity
            let qiya = e.data.now.pressure
            let precip = e.data.now.precip
            that.setData({
              temperature,
              wind,
              tqzk,
              fengli,
              shidu,
              qiya,
              precip
            })
          }
        })
        let furl = futureurl + cityid
        console.log('futureurl:', furl)
        wx.request({
          url: furl,
          success(e) {
            console.log('future', e)
            let fxdate=e.data.daily[0].fxDate
            fxdate=fxdate.split("-")[1]+"-"+fxdate.split("-")[2]
            let tempMin=e.data.daily[0].tempMin
            let tempMax=e.data.daily[0].tempMax
            let textDay=e.data.daily[0].textDay
            let textNight=e.data.daily[0].textNight

            let fxdatee=e.data.daily[1].fxDate
            fxdatee=fxdatee.split("-")[1]+"-"+fxdatee.split("-")[2]
            let tempMinn=e.data.daily[1].tempMin
            let tempMaxx=e.data.daily[1].tempMax
            let textDayy=e.data.daily[1].textDay
            let textNightt=e.data.daily[1].textNight
            
            let fxdateee=e.data.daily[2].fxDate
            fxdateee=fxdateee.split("-")[1]+"-"+fxdateee.split("-")[2]
            let tempMinnn=e.data.daily[2].tempMin
            let tempMaxxx=e.data.daily[2].tempMax
            let textDayyy=e.data.daily[2].textDay
            let textNighttt=e.data.daily[2].textNight
            that.setData({
              fxdate,tempMax,tempMin,textNight,textDay,fxdatee,tempMaxx,tempMinn,textNightt,textDayy,fxdateee,tempMaxxx,tempMinnn,textNighttt,textDayyy
            })
          }
        })
      }
    })
  },
  onReady: function () {
    this.setData({
      city: "广州"
    })
    this.search()
  },
})